package com.Test.com;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class ServerMachine{
    public static void ServerMachine() throws IOException {
        ArrayList<String> allLocalIp = GainAllLocalUSER.ScannerAllLocalIP();
        String serveMachineip = InetAddress.getLocalHost().getHostAddress();
        LinkedList<SerialiableMessage> accountLists=new LinkedList<SerialiableMessage>();
        ServerSocket serverMachine=new ServerSocket(8000);
        System.out.println("服务器启动,开始监听8000端口!");
        while (true) {
            ServeBordcast serveBordcast=new ServeBordcast(allLocalIp,serveMachineip);
            Thread bordServeMachineIp=new Thread(serveBordcast);
            bordServeMachineIp.start();
            Socket accept = serverMachine.accept();
            InetAddress userInetAddress = accept.getInetAddress();
            String userName = userInetAddress.getHostName();
            SerialiableMessage serialiableUser = new SerialiableMessage(userName, userInetAddress.getHostAddress(),"login", "accountList");
            accountLists.add(serialiableUser);
            ServerReceiveRunnable serverReceiveRunnable = new ServerReceiveRunnable(accept);
            ServerSendRunnable serverSendRunnable = new ServerSendRunnable(accept,accountLists);
            Thread receiveThread = new Thread(serverReceiveRunnable);
            Thread sendThread = new Thread(serverSendRunnable);
            sendThread.start();
            receiveThread.start();
        }
    }

    public static void main(String[] args) throws IOException {
        ServerMachine();
    }

    public static class GainAllLocalUSER {
        public static ArrayList<String> ScannerAllLocalIP() throws IOException {
            ArrayList<String> ipList=new ArrayList();
            Runtime runtime=Runtime.getRuntime();
            Process process=runtime.exec("arp -a");
            StringBuilder stringBuilder=new StringBuilder();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
            String line;
            while ((line=bufferedReader.readLine())!=null){
                if (line.length()==0) {
                    int count=0;
                    while (count<3){
                        line=bufferedReader.readLine();
                        count++;
                    }
                }
                String[] split = line.split(" ");
                ipList.add(split[2]);
            }
            return ipList;
        }

        public static ArrayList<String> ScannerAllLocalAccount(ArrayList<String> ipList) throws UnknownHostException {
            ArrayList<String> accountList=new ArrayList<>();
            for (int i = 0; i <ipList.size() ; i++) {
                InetAddress account = InetAddress.getByName(ipList.get(i));
                accountList.add(account.getHostName());
            }
            Iterator<String> iterator=accountList.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
            return accountList;
        }
        public static ArrayList<String> ScannerAllLocalAccount() throws IOException {
            ArrayList<String> ipList = GainAllLocalUSER.ScannerAllLocalIP();
            ArrayList<String> accountList=new ArrayList<>();
            for (int i = 0; i <ipList.size() ; i++) {
                InetAddress account = InetAddress.getByName(ipList.get(i));
                System.out.println(account.getHostName());
                accountList.add(account.getHostName());
            }
            Iterator<String>iterator=accountList.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
            System.out.println("返回本地用户ip组成功!");
            return accountList;
        }
    }
}
